// 把export defualt对象封装起来

import MusicList from '@/components/music-list/music-list.vue'
import { processSongs } from '@/service/song'
// 排行榜、歌单、歌手 详情页
export default function createDetailComponent (name, key, fetch) {
  return {
    name,
    components: { MusicList },
    props: {
      data: Object
    },
    data () {
      return {
        loading: true,
        songs: []
      }
    },
    async created () {
      if (!this.computedData) {
        const path = this.$route.matched[0].path
        this.$router.push(path)
      }
      const result = await fetch(this.computedData)
      // 获取歌曲详情
      this.songs = await processSongs(result.songs)
      this.loading = false
    },
    computed: {
      computedData () {
        let ret = null
        if (this.data) {
          ret = this.data
        } else {
          const cachedSinger = JSON.parse(sessionStorage.getItem(key))
          if (
            cachedSinger &&
            (cachedSinger.mid || cachedSinger.id + '') === this.$route.params.id
          ) {
            ret = cachedSinger
          }
        }
        return ret
      },
      title () {
        const data = this.computedData
        return data.name || data.title
      },
      pic () {
        return this.computedData?.pic
      }
    }
  }
}
